﻿using System.Text;

namespace Fiveesoft.Utility
{
    public class HtmlHelper
    {
        public static string GetPlainTextFromHtmlContent(string html, int numOfCharacters)
        {
            string result = string.Empty;
            if (numOfCharacters == 0)
            {
                StringBuilder allText = new StringBuilder();
                int count = 0;
                bool insideTag = false;
                while (count < html.Length)
                {
                    if (html[count] == '<')
                    {
                        insideTag = true;
                    }
                    else if (html[count] == '>')
                    {
                        insideTag = false;
                    }
                    else
                    {
                        if (!insideTag)
                        {
                            allText.Append(html[count]);
                        }
                    }
                    count++;
                }
                //return all the text
                result = allText.ToString();
            }
            else
            {
                StringBuilder summaryText = new StringBuilder();
                int count = 0;
                bool insideTag = false;
                while (count < html.Length)
                {
                    if (html[count] == '<')
                    {
                        insideTag = true;
                    }
                    else if (html[count] == '>')
                    {
                        insideTag = false;
                    }
                    else
                    {
                        if (!insideTag)
                        {
                            summaryText.Append(html[count]);
                            if (summaryText.Length == numOfCharacters)
                                break;
                        }
                    }
                    count++;
                }
                result = summaryText.ToString();
            }
            return result;
        } 
    }
}